package devicedbgui;
import devicedbgui.GUI;
import devicedbgui.data.RepairInfo;
import devicedbgui.data.RepairInfoDAO;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/** Creates dialog box to add or edit repair information in database */
public class EditRepairInfoDlg extends javax.swing.JDialog {
    /** Reference to GUI class for accessing its variables */
    GUI gui;
    /** Object of RepairInfo class */
    public RepairInfo repairInfo = null;
    /** Checks if information has been modified*/
    public boolean isModifiedDB = false;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /** Constructor creates all components belonging to class */ 
    public EditRepairInfoDlg(GUI gui, RepairInfo repairInfo, boolean modal) {
        super(gui.frame, modal);
        this.gui = gui;
        initComponents();
        setTitle("Add RepairInfo");
        if (repairInfo != null) // edit
        {
            setTitle("Edit RepairInfo");
            this.btnEdit.setText("Update");
            this.repairInfo = repairInfo;
            fillRepairInfo(repairInfo);
        }
        else
            txtTimeentered.setEditable(false);
        txtRepairInfoID.setEditable(false);
        txtBuildID.setText(gui.getBuildId());   
    }
    /** Displays all repair information in Jtable */
    private void fillRepairInfo(RepairInfo repairInfo)
    {
        
        txtRepairInfoID.setText("" + repairInfo.idRepairInfo);
        if (repairInfo.timeEntered != null)
            txtTimeentered.setText(sdf.format(repairInfo.timeEntered));
        txtDescription.setText(repairInfo.description); 
        txtTimeSpent.setText(repairInfo.timeSpent);
        txtChangesMade.setText(repairInfo.changesMade);
        txtCostEstimate.setText(repairInfo.costEstimate);
        txtBuildID.setText("" + repairInfo.Build_idBuild);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jLabel1 = new javax.swing.JLabel();
        txtRepairInfoID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTimeentered = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDescription = new javax.swing.JTextField();
        btnEdit = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtTimeSpent = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtChangesMade = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtCostEstimate = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtBuildID = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("RepairInfo ID");

        jLabel2.setText("Time entered");

        jLabel3.setText("Description");

        btnEdit.setText("Add");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel4.setText("Time spent");

        jLabel5.setText("Changes made");

        jLabel6.setText("Cost estimate");

        jLabel7.setText("Build ID");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, txtBuildID, org.jdesktop.beansbinding.ELProperty.create("false"), txtBuildID, org.jdesktop.beansbinding.BeanProperty.create("editable"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(162, Short.MAX_VALUE)
                .addComponent(btnEdit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel)
                .addGap(194, 194, 194))
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtTimeentered, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtRepairInfoID, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(txtCostEstimate, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtBuildID, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTimeSpent, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(txtChangesMade, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(29, 29, 29))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtBuildID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtRepairInfoID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTimeentered, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTimeSpent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtChangesMade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCostEstimate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnEdit))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
/** Adds or edits repair information in database */
private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
    try {
        RepairInfoDAO dd = new RepairInfoDAO(gui.connection);
        if (repairInfo != null){
            String time = sdf.format(new Date());
            if (!txtTimeentered.getText().equals("")) {
                    time = txtTimeentered.getText();
                }
            repairInfo.timeEntered = sdf.parse(time);
            repairInfo.description = txtDescription.getText();
            repairInfo.timeSpent = txtTimeSpent.getText();
            repairInfo.changesMade = txtChangesMade.getText();
            repairInfo.costEstimate = txtCostEstimate.getText();
            repairInfo.Build_idBuild = Integer.parseInt(txtBuildID.getText());
            
            dd.update(repairInfo);
        }
        else {
            repairInfo = new RepairInfo();
            repairInfo.timeEntered = new Date();
            repairInfo.description = txtDescription.getText();
            repairInfo.timeSpent = txtTimeSpent.getText();
            repairInfo.changesMade = txtChangesMade.getText();
            repairInfo.costEstimate = txtCostEstimate.getText();
            repairInfo.Build_idBuild = Integer.parseInt(txtBuildID.getText());
            dd.insert(repairInfo);
             
        }
    }catch (Exception e){
        JOptionPane.showMessageDialog(gui.frame, e.toString());
        return;
    }
    this.setVisible(false);
    this.dispose();
    isModifiedDB = true;
}//GEN-LAST:event_btnEditActionPerformed
/** Cancel operation for task*/
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.setVisible(false);
    this.dispose();
    isModifiedDB = false;
}//GEN-LAST:event_btnCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnEdit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField txtBuildID;
    private javax.swing.JTextField txtChangesMade;
    private javax.swing.JTextField txtCostEstimate;
    private javax.swing.JTextField txtDescription;
    private javax.swing.JTextField txtRepairInfoID;
    private javax.swing.JTextField txtTimeSpent;
    private javax.swing.JTextField txtTimeentered;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
